package com.linpq.road.controller;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.linpq.common.annotation.Log;
import com.linpq.common.enums.BusinessType;
import com.linpq.road.domain.RdPavementStructure;
import com.linpq.road.service.RdPavementStructureService;
import com.linpq.common.core.controller.BaseController;
import com.linpq.common.core.domain.AjaxResult;
import com.linpq.common.utils.poi.ExcelUtil;
import com.linpq.common.core.page.TableDataInfo;

/**
 * 路面结构Controller
 *
 * @author linpq
 * @date 2021-07-29
 */
@Controller
@RequestMapping("/road/pavement_structure")
public class RdPavementStructureController extends BaseController {
    private String prefix = "road/pavement_structure";

    @Autowired
    private RdPavementStructureService rdPavementStructureService;

    @RequiresPermissions("road:pavement_structure:view")
    @GetMapping()
    public String pavement_structure() {
        return prefix + "/pavement_structure";
    }

    /**
     * 查询路面结构列表
     */
    @RequiresPermissions("road:pavement_structure:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RdPavementStructure rdPavementStructure) {
        startPage();
        List<RdPavementStructure> list = rdPavementStructureService.selectRdPavementStructureList(rdPavementStructure);
        return getDataTable(list);
    }

    /**
     * 导出路面结构列表
     */
    @RequiresPermissions("road:pavement_structure:export")
    @Log(title = "路面结构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RdPavementStructure rdPavementStructure) {
        List<RdPavementStructure> list = rdPavementStructureService.selectRdPavementStructureList(rdPavementStructure);
        ExcelUtil<RdPavementStructure> util = new ExcelUtil<RdPavementStructure>(RdPavementStructure. class);
        return util.exportExcel(list, "pavement_structure");
    }

    /**
     * 新增路面结构
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存路面结构
     */
    @RequiresPermissions("road:pavement_structure:add")
    @Log(title = "路面结构", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RdPavementStructure rdPavementStructure) {
        return toAjax(rdPavementStructureService.insertRdPavementStructure(rdPavementStructure));
    }

    /**
     * 修改路面结构
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        RdPavementStructure rdPavementStructure =rdPavementStructureService.selectRdPavementStructureById(id);
        mmap.put("rdPavementStructure", rdPavementStructure);
        return prefix + "/edit";
    }

    /**
     * 修改保存路面结构
     */
    @RequiresPermissions("road:pavement_structure:edit")
    @Log(title = "路面结构", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RdPavementStructure rdPavementStructure) {
        return toAjax(rdPavementStructureService.updateRdPavementStructure(rdPavementStructure));
    }

    /**
     * 删除路面结构
     */
    @RequiresPermissions("road:pavement_structure:remove")
    @Log(title = "路面结构", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(rdPavementStructureService.deleteRdPavementStructureByIds(ids));
    }

    /**
     * 初始化路面结构
     * @param map
     * @return
     */
    @GetMapping("/form")
    public String form(Model map) {
        Map<String, List> notInitInfoByRdPavementStructure = rdPavementStructureService.getNotInitInfoByRdPavementStructure();
        map.addAttribute("pavementStructure",notInitInfoByRdPavementStructure);
        return prefix + "/stake_form_pavement_structure";
    }
    @RequestMapping(value = "/saveRoadPavementMaterialsInit",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveRoadPavementMaterialsInit(@RequestParam Map data) {
        AjaxResult ajaxResult = rdPavementStructureService.insertPavementMaterialsByRoadSectionName(data);
        return ajaxResult;
    }
}
